package com.mxpio.erp.workshop.controller;

import com.mxpio.erp.common.enums.OrderEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.jpa.model.OrderLine;
import com.mxpio.erp.common.jpa.model.OrderLineKey;
import com.mxpio.erp.common.order.service.OrderLineService;
import com.mxpio.erp.common.vo.NoticeClassVo;
import com.mxpio.erp.common.workshop.entity.*;
import com.mxpio.erp.common.workshop.service.OutsourceOrderItemService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@Tag(name = "OutsourceOrderItemController", description = "委外订单行接口")
@RestController
@RequestMapping("/erp/workshop/ooline/")
public class OutsourceOrderItemController {

	@Autowired
	private OutsourceOrderItemService outsourceOrderItemService;
    
    @Autowired
    private OrderLineService bizOrderLineService;

    @GetMapping("list")
    @Operation(summary = "委外订单行列表", description = "获取委外订单行订单列表（无分页）", method = "GET")
    public Result<List<OutsourceOrderItem>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<OutsourceOrderItem> outsourceOrderItems = outsourceOrderItemService.list(OutsourceOrderItem.class, criteria);
        //处理数量字段
        NoticeClassVo pickingNoticeClazz = new NoticeClassVo(OoPickingNotice.class, OoPickingNoticeLine.class);//领/补/退
        NoticeClassVo defectiveProductsNoticeClazz = new NoticeClassVo(OODefectiveProductsNotice.class, OODefectiveProductsNoticeLine.class);//不良品退料
        List<NoticeClassVo> noticeClassVos = new ArrayList<>();
        noticeClassVos.add(pickingNoticeClazz);
        noticeClassVos.add(defectiveProductsNoticeClazz);
        bizOrderLineService.handleBizOrderLineQuantity(outsourceOrderItems, noticeClassVos, OrderEnums.AccessType.OUT.getCode());
        return Result.OK(outsourceOrderItems);
    }

    @GetMapping("page")
    @Operation(summary = "委外订单行列表", description = "获取委外订单行列表（分页）", method = "GET")
    public Result<Page<OutsourceOrderItem>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<OutsourceOrderItem> outsourceOrderItems = outsourceOrderItemService.listPage(OutsourceOrderItem.class, page, criteria);

        //处理数量字段
        /*NoticeClassVo pickingNoticeClazz = new NoticeClassVo(MoPickingNotice.class, MoPickingNoticeLine.class);//领/补/退
        NoticeClassVo defectiveProductsNoticeClazz = new NoticeClassVo(DefectiveProductsNotice.class, DefectiveProductsNoticeLine.class);//不良品退料
        List<NoticeClassVo> noticeClassVos = new ArrayList<>();
        noticeClassVos.add(pickingNoticeClazz);
        noticeClassVos.add(defectiveProductsNoticeClazz);
        bizOrderLineService.handleBizOrderLineQuantity(outsourceOrderItems.getContent(), noticeClassVos, OrderEnums.AccessType.OUT.getCode());*/

        return Result.OK(outsourceOrderItems);
    }
    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取委外订单行", description = "根据bizNo获取委外订单行", method = "GET")
    public Result<List<OutsourceOrderItem>> getById(@Parameter(description="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<OutsourceOrderItem> outsourceOrderItem = outsourceOrderItemService.list(OutsourceOrderItem.class, c);

        //处理数量字段
        NoticeClassVo pickingNoticeClazz = new NoticeClassVo(OoPickingNotice.class, OoPickingNoticeLine.class);//领/补/退
        NoticeClassVo defectiveProductsNoticeClazz = new NoticeClassVo(OODefectiveProductsNotice.class, OODefectiveProductsNoticeLine.class);//不良品退料
        List<NoticeClassVo> noticeClassVos = new ArrayList<>();
        noticeClassVos.add(pickingNoticeClazz);
        noticeClassVos.add(defectiveProductsNoticeClazz);
        bizOrderLineService.handleBizOrderLineQuantity(outsourceOrderItem, noticeClassVos, OrderEnums.AccessType.OUT.getCode());
        return Result.OK(outsourceOrderItem);
    }

    @GetMapping("list/{bizNo}/{lineNo}")
    @Operation(summary = "根据bizNo获取委外订单行", description = "根据bizNo获取委外订单行", method = "GET")
    public Result<OutsourceOrderItem> getById(@Parameter(description="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo,@Parameter(description="行号") @PathVariable(name = "lineNo", required = true)String lineNo) {
        OrderLineKey lineKey = new OrderLineKey();
        lineKey.setLineNo(lineNo);
        lineKey.setBizNo(bizNo);
        OutsourceOrderItem outsourceOrderItem = outsourceOrderItemService.getById(OutsourceOrderItem.class, lineKey);
        return Result.OK(outsourceOrderItem);
    }

    @RequestMapping("save")
    @Operation(summary = "新增委外订单行", description = "新增委外订单行")
    public Result<OutsourceOrderItem> save(@RequestBody OutsourceOrderItem outsourceOrderItem) {
        outsourceOrderItemService.save(outsourceOrderItem);
        return Result.OK(outsourceOrderItem);
    }
    
    @RequestMapping("batch/save")
    @Operation(summary = "新增委外订单行(批量)", description = "新增委外订单行(批量)")
    public Result<List<OutsourceOrderItem>> save(@RequestBody List<OutsourceOrderItem> outsourceOrderItems) {
        outsourceOrderItemService.save(outsourceOrderItems);
        return Result.OK(outsourceOrderItems);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除委外订单行", description = "删除委外订单行,多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后", method = "DELETE")
    public Result<OutsourceOrderItem> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            String[] pk = key.split(":");
            OrderLineKey lineKey = new OrderLineKey();
            lineKey.setBizNo(pk[0]);
            lineKey.setLineNo(pk[1]);
            outsourceOrderItemService.remove(OutsourceOrderItem.class, lineKey);
        }
        return Result.OK();
    }

    @PutMapping("open/{ids}")
    @Operation(summary = "打开委外订单行", description = "打开委外订单行,多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后", method = "PUT")
    public Result<? extends OrderLine> open(@PathVariable(name = "ids", required = true) String ids) {
        return bizOrderLineService.updateCloseStatus(OutsourceOrderItem.class, ids, OrderStatusEnums.CloseStatus.OPEN);
    }

    @PutMapping("close/{ids}")
    @Operation(summary = "关闭委外订单行", description = "关闭委外订单行", method = "PUT")
    public Result<? extends OrderLine> close(@Parameter(description="业务单号，多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后") @PathVariable(name = "ids", required = true) String ids) {
        return bizOrderLineService.updateCloseStatus(OutsourceOrderItem.class, ids, OrderStatusEnums.CloseStatus.CLOSED);
    }

}
